package eleven.senven.hpb.adapter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.BitmapUtils;

import java.util.List;

import eleven.senven.hpb.R;
import eleven.senven.hpb.bean.MagazintResultBean;

/**
 * Created by Administrator on 2015/7/13.
 */
public class MagazineAdapter extends MyBaseAdapter<MagazintResultBean> {
    private MagazintResultBean data;
    private int num=1;
    private BitmapUtils bitmapUtils;
    private static final int TYPE_COUNT = 2;
    private static final int TYPE_ODD = 0;
    private static final int TYPE_EVEN = 1;
    public MagazineAdapter(List<MagazintResultBean> datas, Context context,BitmapUtils bitmapUtils) {
        super(datas, context);
        this.bitmapUtils=bitmapUtils;
    }

    @Override
    public int getItemViewType(int position) {
        if(position%2==1){
            return TYPE_ODD;
        }else {
            return TYPE_EVEN;
        }
    }

    @Override
    public int getViewTypeCount() {
        return TYPE_COUNT;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        data=datas.get(position);
        ViewHolder viewHolder=null;

        int type = getItemViewType(position);
        if(convertView==null){
            viewHolder=new ViewHolder();
            if(type==TYPE_ODD){
                convertView=inflater.inflate(R.layout.item_magazine_odd,null);
                viewHolder.magImg= (ImageView) convertView.findViewById(R.id.mag_img_odd);
                viewHolder.magTitle= (TextView) convertView.findViewById(R.id.mag_title_odd);
                viewHolder.magContent= (TextView) convertView.findViewById(R.id.mag_content_odd);
                viewHolder.magEditorImg= (ImageView) convertView.findViewById(R.id.mag_idetor_img_odd);
                viewHolder.magEditor= (TextView) convertView.findViewById(R.id.mag_editor_odd);
            }else
            {
                convertView=inflater.inflate(R.layout.item_magazine_even,null);
                viewHolder.magImg= (ImageView) convertView.findViewById(R.id.mag_img_even);
                viewHolder.magTitle= (TextView) convertView.findViewById(R.id.mag_title_even);
                viewHolder.magContent= (TextView) convertView.findViewById(R.id.mag_content_even);
                viewHolder.magEditorImg= (ImageView) convertView.findViewById(R.id.mag_idetor_img_even);
                viewHolder.magEditor= (TextView) convertView.findViewById(R.id.mag_editor_even);
            }
            convertView.setTag(viewHolder);
        }else{
            viewHolder= (ViewHolder) convertView.getTag();
        }
            bitmapUtils.display(viewHolder.magImg,data.getPicture_url());
            viewHolder.magTitle.setText("刊号:"+ data.getSerial_number());
        viewHolder.magContent.setText(data.getFoodzine_name());
            viewHolder.magEditor.setText("主编:"+data.getUser_name());
            bitmapUtils.display(viewHolder.magEditorImg, data.getUser_image());
        Log.i("info",num+"");
        num++;
        return convertView;
    }
    private static class ViewHolder{
        ImageView magImg;
        TextView magTitle;
        TextView magContent;
        TextView magEditor;
        ImageView magEditorImg;
    }
}
